// pages/addressList/addressList.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    on: false,
    addresslist: [],
    hasAdr: true,
    r: 0,//1是购买过来的2购物车过来的3是地址管理过来的
    imgPath:app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("列表:"+options.r)
    this.setData({ r: options.r });
    let self = this;
    //收货地址
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersAddress',
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (header.authorization && header.authorization != app.globalData.authorization) {
          app.globalData.authorization = header.authorization;
        }
        if (data.code == 200) {
          let list = data.data;
          if (list.length == 0) {
            self.setData({ hasAdr: false })
          }
          self.setData({ addresslist: list })
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addAdress:function(){//点击添加地址
    wx.redirectTo({
      url: '/pages/addAddress/addAddress?r=1',
    })
  },
  onChange:function(e){//设为默认
 
      let index = e.currentTarget.dataset.index;
      let self = this;
      // let id = this.data.addresslist[index].id;
      wx.request({
        url: app.globalData.reqPath + 'shop/MembersAddress/' + this.data.addresslist[index].id,
        method: 'PUT',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          id: this.data.addresslist[index].id,
          isDefault: 1,
          addrArea: this.data.addresslist[index].addrArea,
          addrAreaId: this.data.addresslist[index].addrAreaId,
          addrCity: this.data.addresslist[index].addrCity,
          addrCityId: this.data.addresslist[index].addrCityId,
          addrDetail: this.data.addresslist[index].addrDetail,
          addrProvince: this.data.addresslist[index].addrProvince,
          addrProvinceId: this.data.addresslist[index].addrProvinceId,
          receiverName: this.data.addresslist[index].receiverName,
          receiverPhone: this.data.addresslist[index].receiverPhone,
          userId: this.data.addresslist[index].userId,
          zipCode: this.data.addresslist[index].zipCode
        },
        success: function ({ data, header }) {
          if (header.authorization && header.authorization != app.globalData.authorization) {
            app.globalData.authorization = header.authorization;
          }
          if (data.code == 200) {
            console.log(data.data)
            self.onLoad({r:self.data.r});
          }
        }
      })
    
  },
  deladdress: function (e) {
    let id = e.currentTarget.dataset.id;
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/MembersAddress/' + id,
      header: {
        'authorization': app.globalData.authorization
      },
      method: 'DELETE',
      success: function ({ data, header }) {
        console.log(data)
        if (data.code == 200) {
          wx.showToast({
            title: '删除成功',
          })
          self.onLoad({r:self.data.r});
        }
      }
    })
  },
  choose:function(e){
    let aId=e.currentTarget.dataset.id;
    if(this.data.r==2){
      wx.redirectTo({
        url: '/pages/carSuborder/carSuborder?r=2&adrId='+aId,
      })
    } else if (this.data.r == 1){
      wx.redirectTo({
        url: '/pages/suborder/suborder?r=2&adrId=' + aId,
      })
    }
  },
  editaddress:function(e){//地址编辑
    let id=e.currentTarget.dataset.id;
    wx.redirectTo({
      url: '/pages/editAddress/editAddress?id='+id,
    })
  }
})